package com.xiaobo.client;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;

import android.webkit.WebView;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import android.widget.Toast;

import com.xiaobo.util.HttpUtil;

public class SysFpList extends Activity {
	private String account;// 用户帐号
	private String swjgdm;// 机关代码
	// 后台返回的人员信息Key，在以后所有调用后台数据时，
	// 必须把这个Key值传给后台，否则，后台返回null
	private String userKey;
	private String fpdm;// 发票代码
	private String fphm;// 发票号码
	private String url;
	EditText editfpdm;
	EditText editfphm;
	
	ProgressDialog pb1;
	WebView wv;

	ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		Window win = getWindow();
		win.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.sysfplist);
		// 在这儿只需要调用这个方法就可以了

		DataInit();

	}

	void DataInit() {
		// 初始化本表单数据
		Intent intent = this.getIntent();
		Bundle bundle = intent.getExtras();
		account = bundle.getString("account");// 用户名称
		swjgdm = bundle.getString("swjgdm");// 税务机关代码
		// 后台返回的人员信息Key，在以后所有调用后台数据时，
		// 必须把这个Key值传给后台，否则，后台返回null
		userKey = bundle.getString("userKey");

		editfpdm = (EditText) findViewById(R.id.fpdm);
		editfphm = (EditText) findViewById(R.id.fphm);

		
		wv=(WebView)findViewById(R.id.fp_webview);
		wv.setBackgroundColor(0);
    	pb1=new ProgressDialog(SysFpList.this);		
		Button bt = (Button) findViewById(R.id.bt);
		bt.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if (editfpdm.getText().length() < 5) {
					Toast toast = Toast.makeText(getApplicationContext(),
							"请输入发票代码，至少是后五位！", Toast.LENGTH_LONG);
					toast.setGravity(Gravity.CENTER, 0, 0);
					toast.show();
				} else if (editfphm.getText().length() == 0) {
					Toast toast = Toast.makeText(getApplicationContext(),
							"请输入发票号码！", Toast.LENGTH_LONG);
					toast.setGravity(Gravity.CENTER, 0, 0);
					toast.show();
				} else {

			    	pb1.setProgressStyle(ProgressDialog.STYLE_SPINNER);
			    	pb1.setMessage("数据载入中，请稍候！");
					pb1.show();
					fpdm=editfpdm.getText().toString();
					fphm=editfphm.getText().toString();
					url=HttpUtil.IpUrl+ "/server/fplxlist.jsp";
					processThread();// 开始查询信息

				}
			}
		});
		Button bt1 = (Button) findViewById(R.id.bt_kp);
		bt1.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if (editfpdm.getText().length() < 5) {
					Toast toast = Toast.makeText(getApplicationContext(),
							"请输入发票代码，至少是后五位！", Toast.LENGTH_LONG);
					toast.setGravity(Gravity.CENTER, 0, 0);
					toast.show();
				} else if (editfphm.getText().length() == 0) {
					Toast toast = Toast.makeText(getApplicationContext(),
							"请输入发票号码！", Toast.LENGTH_LONG);
					toast.setGravity(Gravity.CENTER, 0, 0);
					toast.show();
				} else {
			    	pb1=new ProgressDialog(SysFpList.this);
			    	pb1.setProgressStyle(ProgressDialog.STYLE_SPINNER);
			    	pb1.setMessage("数据载入中，请稍候！");
					pb1.show();
					fpdm=editfpdm.getText().toString();
					fphm=editfphm.getText().toString();
					url=HttpUtil.IpUrl+ "/server/fpkplist.jsp";
					processThread();// 开始查询信息

				}
			}
		});
		
	}
	// 以下代码是Hanler示例代码，对于耗时的查询需要用以下代码给出进度条

	// 定义Handler直接用，不需要修改
	private Handler handler = new Handler() {
		@Override
		// 当有消息发送出来的时候就执行Handler的这个方法
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			pb1.hide();

  	    }

	};

	// 主进程
	private void processThread() {
		// 构建一个下载进度条
//		pb1 = ProgressDialog.show(PublicWebView.this, "查询银行信息", "正在查询……");
		new Thread() {
			@Override
			public void run() {
				//把耗时长的代码全放在这儿
				

				String result = "";
				// 获得请求对象

				// 调用HttpUtil类中的函数，返回HttpPost
				HttpPost req = HttpUtil.getHttpPost(url);

				List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
				


				params.add(new BasicNameValuePair("fpdm", fpdm));
				params.add(new BasicNameValuePair("fphm", fphm));
				// 所有后台调用都要传这个参数过去
				params.add(new BasicNameValuePair("userKey", userKey));
				// 设置请求对象的编码
				try {
					req.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
					result = HttpUtil.getStringByPost(req);

				} catch (UnsupportedEncodingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					result = "-1";
				}

				wv.loadDataWithBaseURL(null, result, "text/html", "utf-8", null);


				handler.sendEmptyMessage(0);

			}
		}.start();
	}
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			//返回
			pb1.dismiss();
		}
		return super.onKeyDown(keyCode, event);


	}

}
